package com.cskaoyan.service.wx.wxcommentservice;

import com.cskaoyan.bean.bo.wxcommitbo.WxCommentBo;
import com.cskaoyan.bean.bo.wxcommitbo.WxPostCommentBo;
import com.cskaoyan.bean.po.MarketComment;
import com.cskaoyan.bean.po.MarketCommentExample;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.po.MarketUserExample;
import com.cskaoyan.bean.vo.wxcommentvo.CommentVoOfWxCommentVo;
import com.cskaoyan.bean.vo.wxcommentvo.WxCommentCount;
import com.cskaoyan.bean.vo.wxcommentvo.WxCommentVo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.GetInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:todo
 * @Author:todo wang xv
 * @Date:todo 2022/11/23
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    MarketUserMapper userMapper;

    @Autowired
    MarketCommentMapper commentMapper;

    /**
     * @Description:todo 显示商品和专题的评论列表
     * @Author:todo wang xv
     * @Date:todo 2022/11/23
     */
    @Override
    public WxCommentVo queryCommentList(WxCommentBo commentBo) {

        WxCommentVo wxCommentVo = new WxCommentVo();
        // 开启分页；
        PageHelper.startPage(commentBo.getPage(), commentBo.getLimit());

        // 根据接收到的商品id和type找出关于该商品的评价;

        List<MarketComment> comments = commentMapper.selectCommentsByIdDesc(commentBo.getValueId(),commentBo.getType());
        // 根据该商品评论的userId找出对应的username和avatar;
        PageInfo<MarketComment> info = new PageInfo<>(comments);
        wxCommentVo.setLimit(info.getPageSize());
        wxCommentVo.setTotal(info.getTotal());
        wxCommentVo.setPages(info.getPages());
        wxCommentVo.setPage(info.getPageNum());
        List<MarketComment> list = info.getList();

        // 赋值；
        List<CommentVoOfWxCommentVo> commentVos = new ArrayList<>();
        for (MarketComment marketComment : list) {
            Integer userId = marketComment.getUserId();
            MarketUser marketUser = userMapper.selectByPrimaryKey(userId);
            commentVos.add(new CommentVoOfWxCommentVo(marketUser, marketComment.getAddTime(), marketComment.getPicUrls(), marketComment.getAdminContent(), marketComment.getContent()));
        }
        wxCommentVo.setList(commentVos);
        return wxCommentVo;
    }

    @Override
    public WxCommentCount queryCommentCount(Integer valueId, byte type) {
        MarketCommentExample commentExample =new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        criteria.andHasPictureEqualTo(true);
        long hasPicCount = commentMapper.countByExample(commentExample);

        long allCount = commentMapper.countByExample(null);
        WxCommentCount commentCount = new WxCommentCount(hasPicCount, allCount);
        return commentCount;
    }

    @Override
    public MarketComment postComment(WxPostCommentBo postCommentBo) {
        MarketComment comment = new MarketComment();
        comment.setType(postCommentBo.getType());
        comment.setContent(postCommentBo.getContent());
        comment.setStar(postCommentBo.getStar());
        comment.setHasPicture(postCommentBo.isHasPicture());
        comment.setPicUrls(postCommentBo.getPicUrls());
        comment.setAddTime(new Date());
        comment.setUpdateTime(new Date());
        MarketUser info = GetInfo.getWxLoginInfo();
        Integer id = info.getId();
        comment.setUserId(id);
        comment.setValueId(Integer.parseInt(postCommentBo.getValueId()));
        commentMapper.insert(comment);
        return comment;

    }
}
